package com.clearliang.alarmnotice.adapter;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;

import com.clearliang.alarmnotice.R;
import com.clearliang.alarmnotice.base.BaseAdapter;
import com.clearliang.alarmnotice.base.BaseHolder;
import com.clearliang.alarmnotice.bean.AlarmBean;

import java.util.List;

/**
 * Created by ClearLiang on 2019/2/27
 * <p>
 * Function :
 */
public class AlarmAdapter extends BaseAdapter<AlarmBean> {
    private Context mContext;

    public AlarmAdapter(Context context, int layoutId, List<AlarmBean> list) {
        super(layoutId, list);
        mContext = context;
    }

    public interface OnCardItemClickListener {
        void clickSwitchListener(CompoundButton buttonView, AlarmBean item, int position, boolean isChecked);

        void clickListener(AlarmBean item, int position);

        void clickLongListener(AlarmBean item, int position);
    }

    private OnCardItemClickListener onCardItemClickListener;

    public void setOnSwClickListener(OnCardItemClickListener onCardItemClickListener) {
        this.onCardItemClickListener = onCardItemClickListener;
    }

    @Override
    protected void convert(BaseHolder holder, final AlarmBean item) {
        final int position = (int) holder.itemView.getTag();
        holder.setText(R.id.tv_name, item.getName());
        holder.setText(R.id.tv_content, item.getContent());
        holder.setText(R.id.tv_time, item.getStartTime().substring(0, 5) + "-" + item.getEndTime().substring(0, 5));
        StringBuilder msg = new StringBuilder();
        for (String a : item.getWeekday()) {
            msg.append(a).append("|");
        }
        holder.setText(R.id.tv_weekday, msg.toString());

        CardView cardView = holder.getView(R.id.cv_item);
        if (item.getName().contains("数学")) {
            cardView.setCardBackgroundColor(mContext.getResources().getColor(R.color.app_color_theme_3));
        } else if (item.getName().contains("英语")) {
            cardView.setCardBackgroundColor(mContext.getResources().getColor(R.color.app_color_theme_4));
        } else if (item.getName().contains("专业")) {
            cardView.setCardBackgroundColor(mContext.getResources().getColor(R.color.app_color_theme_5));
        } else if (item.getName().contains("政治")) {
            cardView.setCardBackgroundColor(mContext.getResources().getColor(R.color.app_color_theme_6));
        } else {
            cardView.setCardBackgroundColor(mContext.getResources().getColor(R.color.app_color_theme_9));
        }

        cardView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                onCardItemClickListener.clickLongListener(item, position);
                return false;
            }
        });
        cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onCardItemClickListener.clickListener(item, position);
            }
        });

        Switch sw = (Switch) holder.getView(R.id.switch1);
        sw.setChecked(item.isIsOpen());
        sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                onCardItemClickListener.clickSwitchListener(buttonView, item, position, isChecked);
            }
        });
    }
}
